package com.lab.jumper;

import android.content.Intent;
import android.os.Bundle;


import com.lab.logtrack.AppLog;

import java.util.HashMap;

/**
 * Created by ak
 * Bundle管理中心,用于处理组件之间传递值的引用和备份管理
 */
public class BundleCenter {

    private static BundleCenter sInstance;

    public static BundleCenter getInstance() {
        if (null == sInstance)
            sInstance = new BundleCenter();
        return sInstance;
    }

    public static final String DATA_TAG = "bundle_center_data_tag";
    private static final int RANDOM_FACTOR = 1000;
    private HashMap<String, Bundle> mMapTagBundle = new HashMap<String, Bundle>();

    private BundleCenter() {
    }

    /**
     * 包装原Bundle
     * @param bundle
     * @param tag
     * @return
     */
    public Bundle wrapBundle(Bundle bundle, String tag) {
        if (null != bundle) {
            String dataTag = generateDataTag(tag);
            bundle.putString(DATA_TAG, dataTag);
            mMapTagBundle.put(dataTag, bundle);
            AppLog.d("Bundle Center wrapBundle dataTag,bundle = " + dataTag + "," + bundle);
            return bundle;
        }
        return null;
    }

    /**
     * 包装原Bundle
     * @param bundle
     * @return
     */
    public Bundle wrapBundle(Bundle bundle) {
        return wrapBundle(bundle, null);
    }

    /**
     * 包装原Bundle
     * @param intent
     * @param tag
     */
    public void wrapIntent(Intent intent, String tag) {
        if (null != intent) {
            Bundle bundle = intent.getExtras();
            if (null != bundle) {
                intent.putExtras(wrapBundle(bundle, tag));
            }
        }
    }

    /**
     * 包装原Bundle
     * @param intent
     */
    public void wrapIntent(Intent intent) {
        wrapIntent(intent, null);
    }

    /**
     * 移除原Bundle
     * @param dataTag
     */
    public void removeBundle(String dataTag) {
        if (null != dataTag) {
            Bundle bundle = mMapTagBundle.remove(dataTag);
            AppLog.d("Bundle Center removeBundle dataTag,bundle = " + dataTag + "," + bundle);
        }
    }

    /**
     * 移除原Bundle
     * @param bundle
     */
    public void removeBundle(Bundle bundle) {
        if (null != bundle) {
            String dataTag = bundle.getString(DATA_TAG);
            removeBundle(dataTag);
        }
    }

    /**
     * 移除原Bundle
     * @param intent
     */
    public void removeBundle(Intent intent) {
        if (null != intent) {
            removeBundle(intent.getExtras());
        }
    }

    /**
     * 获取原Bundle
     * @param dataTag
     * @return
     */
    public Bundle getBundle(String dataTag) {
        if (null != dataTag) {
            Bundle bundle = mMapTagBundle.get(dataTag);
            AppLog.d("Bundle Center getBundle dataTag,bundle = " + dataTag + "," + bundle);
            return bundle;
        }
        return null;
    }

    /**
     * 清除原Bundle列表
     */
    public void clear(){
        mMapTagBundle.clear();
    }

    /**
     * 产生DataTag信息
     * @param tag
     * @return
     */
    private String generateDataTag(String tag) {
        StringBuilder tagBuilder = new StringBuilder();
//        tagBuilder.append(System.currentTimeMillis()).append("-").append(Math.random() * RANDOM_FACTOR);
        tagBuilder.append(System.currentTimeMillis()).append("-");
        if (null != tag)
            tagBuilder.append(tag);
        return tagBuilder.toString();
    }

}
